package com.graphsql.gamesdk.activity;

import java.util.ArrayList;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.graphsql.gamesdk.R;
import com.graphsql.gamesdk.adapter.MyFragmentPagerAdapter;
import com.graphsql.gamesdk.base.BaseFragment;
import com.graphsql.gamesdk.base.BaseFragmentActivity;
import com.graphsql.gamesdk.fragment.GSHomeFragment;
import com.graphsql.gamesdk.util.LogDebugger;

public class GSMainFragmentActivity extends BaseFragmentActivity {
    private static final String TAG = "GSMainFragmentActivity";
    
	private ActionBar actionBar;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	private ViewPager mViewPager;
	
    private MyFragmentPagerAdapter mAdapter;
    private ArrayList<Fragment> fragmentsList;

    private ImageView ivBottomLine;
    private TextView ind_1, ind_2,ind_3,ind_4;
    private int currIndex = 0;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    private int position_two;
    private int position_three;
    
    private  int color1,color2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gs_activity_main);
		// Set up the action bar.
		actionBar = getSupportActionBar();
		actionBar.setIcon(R.drawable.gs_main_logo);
		actionBar.setTitle(R.string.gs_string_app_name);
		InitView();
        InitWidth();
        InitViewPager();
	}
	private void InitViewPager() {
		mViewPager = (ViewPager) findViewById(R.id.gs_pager);
		mViewPager.setOffscreenPageLimit(4);
        fragmentsList = new ArrayList<Fragment>();

        GSHomeFragment fragment1 = new GSHomeFragment();//排行
        GSHomeFragment fragment2 = new GSHomeFragment();//推荐
        GSHomeFragment fragment3 = new GSHomeFragment();//排行
        GSHomeFragment fragment4 = new GSHomeFragment();//最新
        fragmentsList.add(fragment1);
        fragmentsList.add(fragment2);
        fragmentsList.add(fragment3);
        fragmentsList.add(fragment4);
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        fragment1.onPrepareData();
    }
	
	private void InitView() {
		color1 = this.getResources().getColor(R.color.gs_color_black);
		color2 = this.getResources().getColor(R.color.gs_color_orange);
    	ind_1 = (TextView) findViewById(R.id.gs_txt_ind_1);
    	ind_2 = (TextView) findViewById(R.id.gs_txt_ind_2);
    	ind_3 = (TextView) findViewById(R.id.gs_txt_ind_3);
    	ind_4 = (TextView) findViewById(R.id.gs_txt_ind_4);

    	ind_1.setOnClickListener(new MyOnClickListener(0));
    	ind_2.setOnClickListener(new MyOnClickListener(1));
    	ind_3.setOnClickListener(new MyOnClickListener(2));
    	ind_4.setOnClickListener(new MyOnClickListener(3));

        ivBottomLine = (ImageView) findViewById(R.id.gs_bottom_line);
    }
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        InitWidth();
    }
	private void InitWidth() {
    	DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        bottomLineWidth = screenW/4-20;
        offset = screenW/8 - bottomLineWidth/ 2;
        position_one = screenW/4;
        position_two =  position_one + screenW/4;
        position_three =  position_two + screenW/4;
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)ivBottomLine.getLayoutParams();
        lp.width = bottomLineWidth;
        lp.setMargins(offset,0,0,0);
        ivBottomLine.setLayoutParams(lp);
//        ivBottomLine.layout(200, 0, 300, 3);
        Animation animation = null;//转屏不能正常显示
        if(currIndex==1){
        	animation = new TranslateAnimation(position_one, position_one, 0, 0);
        }else if(currIndex==2){
        	animation = new TranslateAnimation(position_two, position_two, 0, 0);
        }else if(currIndex==3){
        	animation = new TranslateAnimation(position_three, position_three, 0, 0);
        }
        if(currIndex!=0){
            animation.setFillAfter(true);
            animation.setDuration(300);
            ivBottomLine.startAnimation(animation);
        }
    }
	public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
        	mViewPager.setCurrentItem(index);
        }
    };

	
	
	public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            ind_1.setTextColor(color1);
            ind_2.setTextColor(color1);
            ind_3.setTextColor(color1);
            ind_4.setTextColor(color1);
            switch (arg0) {
            case 0:
            	 if (currIndex == 1) {
                     animation = new TranslateAnimation(position_one, 0, 0, 0);
                 } else if (currIndex == 2) {
                     animation = new TranslateAnimation(position_two, 0, 0, 0);
                 } else if (currIndex == 3) {
                     animation = new TranslateAnimation(position_three, 0, 0, 0);
                 }
                ind_1.setTextColor(color2);
                break;
            case 1:
            	 if (currIndex == 0) {
                     animation = new TranslateAnimation(offset, position_one, 0, 0);
                 } else if (currIndex == 2) {
                     animation = new TranslateAnimation(position_two, position_one, 0, 0);
                 } else if (currIndex == 3) {
                     animation = new TranslateAnimation(position_three, position_one, 0, 0);
                 }
                ind_2.setTextColor(color2);
                break;
            case 2:
            	if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, position_two, 0, 0);
                 } else if (currIndex == 1) {
                    animation = new TranslateAnimation(position_one, position_two, 0, 0);
                } else if (currIndex == 3) {
                    animation = new TranslateAnimation(position_three, position_two, 0, 0);
                }
                ind_3.setTextColor(color2);
                break;
            case 3:
            	 if (currIndex == 0) {
                     animation = new TranslateAnimation(offset, position_three, 0, 0);
                 } else if (currIndex == 1) {
                     animation = new TranslateAnimation(position_one, position_three, 0, 0);
                 } else if (currIndex == 2) {
                     animation = new TranslateAnimation(position_two, position_three, 0, 0);
                 }
                ind_4.setTextColor(color2);
                break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(300);
            ivBottomLine.startAnimation(animation);
            try{
	            BaseFragment f = (BaseFragment)mAdapter.getItem(currIndex);
	            f.onPrepareData();//载入数据，防止同时全部载入
            }catch (Exception e) {
            	LogDebugger.error(TAG, "载入数据 Exception "+e.toString());
			}
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}
